﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using VersionComparer.Views;

namespace VersionComparer.DataModel.Actions
{
    [ActionAttribute("Follow link",1, System.Windows.Forms.Keys.Control | System.Windows.Forms.Keys.Enter)]
    public class LinkAction : SpecificAction
    {
        public LinkAction():base()
        {
        }

        public override bool RefreshAfterFinish
        {
            get
            {
                return false;
            }
            set
            {
            }
        }
        public override bool MayBeExecuted(MainForm mainForm, ComparableItem[] focusedSelectedItems, ComparableItem[] unfocusedSelectedItems)
        {
            if (focusedSelectedItems.Length != 1)
                return false;

            foreach (ComparableItem item in focusedSelectedItems)
            {
                try
                {
                    if (Uri.IsWellFormedUriString(item.Text, UriKind.Absolute))
                        return true;
                }
                catch
                {
                }
            }
            return false;
        }

        public override System.Drawing.Image Image(MainForm mainForm, ComparableItem[] focusedSelectedItems, ComparableItem[] unfocusedSelectedItems)
        {
            string ieLocation = string.Format (  "{0}\\Internet Explorer\\iexplore.exe", System.Environment.GetFolderPath(System.Environment.SpecialFolder.ProgramFiles));
            return Icon.ExtractAssociatedIcon(ieLocation).ToBitmap();
        }

        protected override void ExecuteAction(MainForm mainForm, ComparableItem[] focusedSelectedItems, ComparableItem[] unfocusedSelectedItems)
        {
            foreach (var item in focusedSelectedItems)
            {
                System.Diagnostics.Process process = new System.Diagnostics.Process();
                
                string ieLocation = string.Format (  "\"{0}\\Internet Explorer\\iexplore.exe\"", System.Environment.GetFolderPath(System.Environment.SpecialFolder.ProgramFiles));
                process.StartInfo = new System.Diagnostics.ProcessStartInfo(ieLocation, "\"" + item.Text + "\"");
                
                process.Start();
            }
        }

    }
}
